﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.Threading;
using System.IO;
using System.Collections.Specialized;
using FightLand_Sever.Model.Net;
using System.Management;

namespace FightLand_Sever
{
    class Program
    {
        static void Main(string[] args)
        {
            bool restart = false;
            try { FireWall.NetFwAddPort("斗地主端口", 8080, "TCP"); } catch (Exception) { }
            do
            {
                restart = false;
                WebListener.StartListen();
                Management.StartListen();
                while (true)
                {
                    string[] cmds = Console.ReadLine().Split(' ');
                    string head = cmds[0];
                    string value = "";
                    if (cmds.Length > 1) value = cmds[1];
                    switch (cmds[0])
                    {
                        case "help":
                            {
                                ShowHelp();
                                break;
                            }
                        case "clear":
                            {
                                Console.Clear();
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                Console.Write("\nEnter Command:> ");
                                Console.ForegroundColor = ConsoleColor.White;
                                break;
                            }
                        case "exit":
                            {
                                goto whileout;
                            }
                        case "restart":
                            {
                                restart = true;
                                Print("重启服务器...");
                                goto whileout;
                            }
                        case "offline":
                            {
                                var p = Management.GetOnlinePlayerByID(value);
                                if (p == null) break;
                                p.ClocseWebSok();
                                Print("已将玩家 " + value + " 连接断开");
                                break;
                            }
                        case "pinfo":
                            {
                                var v = cmds.Getcv(1);
                                if (v.Length == 0)
                                {
                                    NoCmd(cmds.Getcv(0));
                                    return;
                                }
                                var ip = cmds.Getcv(1);
                                if (Management.GetPlayerDataInAll(ip) != null)
                                {
                                    var p = Management.GetPlayerDataInAll(ip);
                                    Print(string.Format("玩家姓名:{0}\n" +
                                        "玩家ID:{1}\n" +
                                        "剩余欢乐币:{2}\n" +
                                        "IP地址:{3}\n" +
                                        "玩家性别:{4}\n", p.Name, p.PlayerID, p.Mark, p.IP, p.Gender == 0 ? "男" : "女"));
                                }
                                else
                                {
                                    ShowError("没有查询到该玩家地址信息!");
                                }
                                break;
                            }
                        case "sysinfo":
                            {
                                Task.Run(() =>
                                {
                                    var ninfo = Tools.Sys.GetISCapacity();
                                    Print(string.Format("CPU:{0}\n" +
                                        "总内存:{1}\n" +
                                        "内存使用率:{2}%\n",
                                        Tools.Sys.GetCPU(),
                                        Tools.Sys.GetMemorySize(),
                                        ninfo.dwMemoryLoad));
                                });
                                break;
                            }
                        case "addr":
                            {
                                Print("访问地址: http://" + Tools.GetLocalIP() + ":8080");
                                break;
                            }
                        case "mmtinfo":
                            {
                                Print(string.Format("在线成员数:{0}\n" +
                                    "注册成员数:{1}\n" +
                                    "进行中游戏数:{2}\n" +
                                    "房间数:{3}",
                                    Management.OnlineCount,
                                    Management.RegCount,
                                    Management.RunGameCount,
                                    Management.RoomsCount));
                                break;
                            }
                        case "sendbar":
                            {
                                var mes = cmds.Getcv(1);
                                if (!string.IsNullOrEmpty(mes))
                                {
                                    Management.SendBarrage("[系统]", mes, true);
                                    Print("通告发送成功!");
                                }
                                else
                                {
                                    ShowError("通告内容不能为空白");
                                }
                                break;
                            }
                        default:
                            {
                                NoCmd(cmds.Getcv(0));
                                break;
                            }
                    }
                }
            whileout:
                WebListener.Dispose();
                Print("Http服务已关闭");
            } while (restart);
            Print("服务器程序结束...");
            Console.Read();
        }

        static void NoCmd(string cmd)
        {
            Console.Write("\n\"" + cmd + "\" 不是命令,您可以输入以下命令:\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("\nEnter Command:> ");
            Console.ForegroundColor = ConsoleColor.White;
            ShowHelp();
        }

        static void ShowError(string mes)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\n" + mes + "\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("\nEnter Command:> ");
            Console.ForegroundColor = ConsoleColor.White;
        }

        static void ShowHelp()
        {
            Print("clear: 清除控制台\n" +
                                    "exit：退出服务器\n" +
                                    "restart: 重启服务器\n" +
                                    "offline (玩家ID): 强制下线玩家\n" +
                                    "pinfo (玩家IP): 输出玩家信息\n" +
                                    "sysinfo: 显示服务器硬件状态\n" +
                                    "addr: 显示大厅访问地址\n" +
                                    "mmtinfo: 显示当前服务数据信息\n" +
                                    "sendbar (发送内容): 发送系统公告");
        }

        static void Print(string mes)
        {
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.Write(new string(' ', Console.WindowWidth / 3) + "\n");
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(0, Console.CursorTop);
            Console.WriteLine(mes);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("\nEnter Command:> ");
            Console.ForegroundColor = ConsoleColor.White;
        }
    }

    static class Ext
    {
        public static string Getcv(this string[] cmds, int index)
        {
            return cmds.Length > index ? cmds[index] : "";
        }
    }
}
